package com.cungmua.app.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.adapter.ListMapAddressAdapter;
import com.cungmua.app.base.BaseDrawerActivitys;
import com.cungmua.app.handler.DealDetailsHandler;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.DealDetails;
import com.cungmua.app.model.request.DealDetailsRequest;
import com.cungmua.app.model.response.DealDetailsResponse;
import com.cungmua.app.task.DealDetailsTask;
import com.cungmua.app.utils.AppUtils;

/**
 * Created by kelvin.nguyen on 6/27/14.
 */
public class MapAddressActivity extends BaseDrawerActivitys implements DealDetailsHandler{
    private int currentDealId;
    private ImageView icon_home;
    private ImageView btn_logo;
    private Button icon_cart;
    private TextView txtTitelFeatured;
    private TextView txtHighLight;
    private WebView txtContent;
    private LinearLayout btnCart;
    private String titleHighLight;
    private TextView txtMarketPrice;
    private TextView txtPrice;
    private TextView txt_header_title;
    private ImageButton btnBuy;
    private DealDetails details;
    private ListView listViewMapAddress;
    private ListMapAddressAdapter mAdapter;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_map_address;
    }

    public MapAddressActivity() {
    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        super.loadControls(savedInstanceState);
        currentDealId = getIntent().getExtras().getInt(IK_DEAL_ID);
        titleHighLight=getIntent().getExtras().getString(KEY_TITLE);

        txtMarketPrice = (TextView) findViewById(R.id.txtMarketPrice);
        txtMarketPrice.setPaintFlags(txtMarketPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        txtPrice = (TextView) findViewById(R.id.txtPrice);
        btnBuy = (ImageButton) findViewById(R.id.btnBuy);
        icon_home=(ImageView)findViewById(R.id.icon_home);
        btn_logo=(ImageView)findViewById(R.id.btn_logo);
        icon_cart=(Button)findViewById(R.id.icon_cart);
//        txtTitelFeatured=(TextView)findViewById(R.id.txtTitelFeatured);
        txtHighLight=(TextView)findViewById(R.id.txtHighLight);
        txt_header_title=(TextView)findViewById(R.id.txt_header_title);
        txtContent=(WebView)findViewById(R.id.txtContent);
        btnCart=(LinearLayout)findViewById(R.id.btnCart);
        listViewMapAddress=(ListView)findViewById(R.id.listViewMapAddress);

        mAdapter=new ListMapAddressAdapter(this);



        icon_home.setVisibility(View.GONE);
        btnCart.setVisibility(View.GONE);

        Cart currentCart=getCurrentCart();
        icon_cart.setText(currentCart.getTotalQuantity()+"");

        icon_home.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToYourCart();
            }
        });

        txt_header_title.setVisibility(View.VISIBLE);
        btn_logo.setVisibility(View.GONE);
    }

    @Override
    public void updateView() {
        DealDetailsRequest request = new DealDetailsRequest(getDeviceId(), getToken());
        request.setCityId(getCurrentCity().getId());
        request.setUserId(getCurrentUser().getId());
        request.setDealId(currentDealId);

        DealDetailsTask task = new DealDetailsTask(this, this);
        task.execute(request);
    }

    @Override
    public void onDealDetails(DealDetailsResponse response) {

        details = response.getData();

        String priceString = getString(R.string.full_price_string, AppUtils.getFullPriceString(details.getMarketPrice()));
        txtMarketPrice.setText(priceString);
        priceString = getString(R.string.full_price_string, AppUtils.getFullPriceString(details.getPrice()));
        txtPrice.setText(priceString);

//        txtTitelFeatured.setText(titleHighLight+"");

        txt_header_title.setText(titleHighLight);

        mAdapter.setDataSource(details.getMapAddressList());

        listViewMapAddress.setAdapter(mAdapter);


        switch (details.getDealStatus()) {
            case DealDetails.ACTIVE:
                btnBuy.setImageResource(R.drawable.selector_button_buy);
                break;
            case DealDetails.COMING_SOON:
                btnBuy.setImageResource(R.drawable.selector_button_coming_soon);
                break;
            case DealDetails.SOLD_OUT:
                btnBuy.setImageResource(R.drawable.selector_button_out_of_stock);
                break;
            case DealDetails.EXPIRED:
                btnBuy.setImageResource(R.drawable.selector_button_expired);
                break;
            case DealDetails.INACTIVE:
                btnBuy.setImageResource(R.drawable.selector_button_cancelled);
                break;
        }
        if (details.getDealStatus() != DealDetails.ACTIVE)
            btnBuy.setEnabled(false);

        btnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goToBuy(details);
            }
        });
    }
}
